package account
/**
客服
*/
import (
  "bytes"
  "encoding/json"
  "errors"
  "fmt"
  "gitee.com/yizhisamoye/GoWeChat/entity/customerSupport"
  "gitee.com/yizhisamoye/GoWeChat/utils"
  "io"
  "mime/multipart"
  "net/http"
  "os"
)
/**
添加客服
 */
func(w WxAccount)AddCustomerSupport(kfAccount string,nickname string)error{
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfaccount/add?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]string{
    "kf_account":kfAccount,
    "nickname":nickname,
  })
  if err!=nil{
    return err
  }
  return nil
}
/**
更新客服
 */
func(w WxAccount)UpdateCustomerSupport(kfAccount string,nickname string)error{
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfaccount/update?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]string{
    "kf_account":kfAccount,
    "nickname":nickname,
  })
  if err!=nil{
    return err
  }
  return nil
}
/**
删除客服
 */
func(w WxAccount)DeleteCustomerSupport(kfAccount string)error{
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfaccount/del?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]string{
    "kf_account":kfAccount,
  })
  if err!=nil{
    return err
  }
  return nil
}
/**
获取所有客服
 */
func(w WxAccount)GetAllCustomerSupport()([]customerSupport.WxCustomerSupport,error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/customservice/getkflist?access_token=%s",w.accessToken)
  resp,err:=http.Get(url)
  if err!=nil{
    return nil,err
  }
  result,err:= utils.CheckWxResp(resp)
  if err!=nil{
    return nil,err
  }
  kfList:=make([]customerSupport.WxCustomerSupport,0)
 if v,exist:=result["kf_list"];exist{
   for _,kf:=range v.([]interface{}){
     kfData:=kf.(map[string]string)
     kfDataByte,_:=json.Marshal(kfData)
     kfEntity:= new(customerSupport.WxCustomerSupport)
     json.Unmarshal(kfDataByte,kfEntity)
     kfList=append(kfList,*kfEntity)
   }
   return kfList,nil
 }
  return nil,errors.New("客服数据字段不存在")
}
/**
获取在线客服
*/
func(w WxAccount)GetOnLineCustomerSupport()([]customerSupport.WxCustomerSupport,error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/customservice/getonlinekflist?access_token=%s",w.accessToken)
  resp,err:=http.Get(url)
  if err!=nil{
    return nil,err
  }
  result,err:= utils.CheckWxResp(resp)
  if err!=nil{
    return nil,err
  }
  kfOnlineList:=make([]customerSupport.WxCustomerSupport,0)
  if v,exist:=result["kf_online_list"];exist{
    for _,kf:=range v.([]interface{}){
      kfData:=kf.(map[string]string)
      kfDataByte,_:=json.Marshal(kfData)
      kfEntity:= new(customerSupport.WxCustomerSupport)
      json.Unmarshal(kfDataByte,kfEntity)
      kfOnlineList=append(kfOnlineList,*kfEntity)
    }
    return kfOnlineList,nil
  }
  return nil,errors.New("在线客服数据字段不存在")
}
/**
上传客服头像
 */
func(w WxAccount)UploadCustomerSupportAvatar(kfAccount string,file *os.File)error{
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfaccount/uploadheadimg?access_token=%s&kf_account=%s",w.accessToken,kfAccount)
  buf:=new(bytes.Buffer)
 writer:= multipart.NewWriter(buf)
  formFileWriter,err:=writer.CreateFormFile("media",file.Name())
  if err!=nil{
    return err
  }
  _,err=io.Copy(formFileWriter,file)
  if err!=nil{
    return err
  }
  contentType:=writer.FormDataContentType()
  writer.Close()
  resp,err:=http.Post(url,contentType,buf)
  if err!=nil{
    return err
  }
  _,err= utils.CheckWxResp(resp)
  return err
}
//发送消息
func(w WxAccount)SendCustomerSupportMsg(msg customerSupport.WxCustomerSupportMsg)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/message/custom/send?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,msg)
  return err
}
//更新正在输入状态
func(w WxAccount)CustomerSupportTyping(openId string)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/message/custom/typing?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]string{
    "touser":openId,
    "command":"Typing",
  })
  return err
}
/**
邀请绑定客服账号
 */
func(w WxAccount)InviteBind(kfAccount string,inviteWx string)error{
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfaccount/inviteworker?access_token=%s",w.accessToken)
 _,err:=utils.SendJson(url,map[string]string{
    "kf_account":kfAccount,
    "invite_wx":inviteWx,
  })
 return err
}


/**********会话控制**************/

/**
开启会话
 */
func(w WxAccount)OpenCustomerSupportSession(kfAccount string,openId string)error{
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfsession/create?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]string{
    "kf_account":kfAccount,
    "openid":openId,
  })
  return err
}
/**
关闭会话
 */
func(w WxAccount)CloseCustomerSupportSession(kfAccount string,openId string)error{
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfsession/close?access_token=%s",w.accessToken)
  _,err:=utils.SendJson(url,map[string]string{
    "kf_account":kfAccount,
    "openid":openId,
  })
  return err
}
/**
获取会话
 */
func(w WxAccount)GetCustomerSupportSession(openId string)(*customerSupport.WxCustomerSupportSession,error){
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfsession/getsession?access_token=%s&openid=%s ",w.accessToken,openId)
  result,err:= utils.GetJson(url)
  if err!=nil{
    return nil,err
  }
  session:=new(customerSupport.WxCustomerSupportSession)
  resultByte,_:=json.Marshal(result)
  json.Unmarshal(resultByte,session)
  return session,nil
}
/**
获取客服会话列表
 */
func(w WxAccount)GetAllCustomerSupportSession(kfAccount string)([]customerSupport.WxCustomerSupportSession,error){
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfsession/getsessionlist?access_token=%s&kf_account=%s",w.accessToken,kfAccount)
  result,err:=utils.GetJson(url)
  if err!=nil{
    return nil,err
  }
  sessionList:=result["sessionlist"].([]interface{})
  resultSessionList:=make([]customerSupport.WxCustomerSupportSession,0)
  for _,v:=range sessionList{
    session:=new(customerSupport.WxCustomerSupportSession)
    resultByte,_:=json.Marshal(v)
    json.Unmarshal(resultByte,session)
    resultSessionList=append(resultSessionList,*session)
  }
  return resultSessionList,nil
}
/**
获取未接入会话列表
 */
func(w WxAccount)GetWaitCustomerSupportSession()([]customerSupport.WxCustomerSupportSession,int,error){
  url:=fmt.Sprintf(BaseUrl+"/customservice/kfsession/getwaitcase?access_token=%s",w.accessToken)
  result,err:=utils.GetJson(url)
  if err!=nil{
    return nil,0,err
  }
  waitList:=result["waitcaselist"].([]interface{})
  resultWaitList:=make([]customerSupport.WxCustomerSupportSession,0)
  for _,v:=range waitList{
    session:=new(customerSupport.WxCustomerSupportSession)
    resultByte,_:=json.Marshal(v)
    json.Unmarshal(resultByte,session)
    resultWaitList=append(resultWaitList,*session)
  }
  count:=int(result["count"].(float64))
  return resultWaitList,count,nil
}

/**
获取聊天记录
 */
func(w WxAccount)GetCustomerSupportMsgRecord(startTime,endTime,msgId,number int)(list []customerSupport.WxCustomerSupportMsgRecord,listNumber int,nextMsgId int,err error){
  url:=fmt.Sprintf(BaseUrl+"/customservice/msgrecord/getmsglist?access_token=%s",w.accessToken)
  result,err:=utils.SendJson(url,map[string]int{
    "starttime":startTime,
    "endtime":endTime,
    "msgid":msgId,
    "number":number,
  })
  if err!=nil{
    return nil,0,0,err
  }
  recordList:=result["recordlist"].([]interface{})
  for _,v:=range recordList{
    record:=new(customerSupport.WxCustomerSupportMsgRecord)
    recordBytes,_:=json.Marshal(v)
    json.Unmarshal(recordBytes,record)
    list=append(list,*record)
  }
  return list,int(result["number"].(float64)),int(result["msgid"].(float64)),nil
}

